package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"time"
	"os"
)

type Config struct {
	CrawleInterval     time.Duration `json:"crawle_interval"`
	CrawleNextInterval time.Duration `json:"crawle_next_interval"`
	CrawleRecentlyMax  time.Duration `json:"crawle_recently_max"`

	HTTPTimeout      time.Duration `json:"http_timeout"`
	WechatKey        string        `json:"wechat_key"`
	ServerChanSKey   string        `json:"serverchan_skey"`
	WxPusherAppToken string        `json:"wxpusher_app_token"`
	WxPusherTopicIDs []int64       `json:"wxpusher_topic_ids"`
	WxPusherUIDs     []string      `json:"wxpusher_uids"`

	WorthyMin      int64    `json:"worthy_min"`
	CollectionMin  int64    `json:"collection_min"`
	CommentMin     int64    `json:"comment_min"`
	RMBMax         int64    `json:"rmb_max"`
	CategoryExcept []string `json:"category_except"`
	TitleExcept    []string `json:"title_except"`
	TitlePass      []string `json:"title_pass"`
}

var cfg Config

func init() {
	cfg = Config{
		CrawleInterval:     time.Second * 10,
		CrawleNextInterval: time.Minute * 2,
		CrawleRecentlyMax:  time.Hour * 2,

		HTTPTimeout:      time.Second * 20,
		WechatKey:        "763863bc-3604-4409-b336-a9216f6827c3",
		ServerChanSKey:   "SCU111897T43c0426eda0dc05f78e73cc2c3edad5d5f4f6429e66b8",
		WxPusherAppToken: "AT_eGWfOqxodn66SWHyRzZNjTz1xT7UVhM9",
		WxPusherTopicIDs: []int64{863},
		WxPusherUIDs:     []string{"UID_XtYJ0OJ4kFDvJGvxdsPLZGJbjhQU"},

		WorthyMin:      8,
		CollectionMin:  30,
		CommentMin:     8,
		RMBMax:         10000,
		CategoryExcept: []string{"母婴用品", "服饰鞋包", "汽车消费", "个护化妆", "运动户外"},
		TitleExcept:    []string{"鞋", "衫", "裤", "袜", "夹克", "啤", "红酒", "白酒", "葡萄酒"},
		TitlePass:      []string{"BUG价", "历史低价", "手慢无", "绝对值", "神价格", "值友专享", "京豆"},
	}
}

func initConfig() {
	cfgFile := "config.json"
	if len(os.Args) > 1 {
		cfgFile = os.Args[1]
		log.Println(cfgFile)
	}

	if cfgData, err1 := ioutil.ReadFile(cfgFile); nil == err1 {
		var cfgJSON Config
		if err2 := json.Unmarshal(cfgData, &cfgJSON); nil == err2 {
			cfg = cfgJSON
		}
	}

	log.Printf("%+v", cfg)
}
